﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Media;
using System.Windows.Input;

namespace PresentationProject
{
    class PlateImage:ImageObject
    {
        private Point previousPosition;
        private bool isToMove = false;
        private Thickness previousMargin;
        private Point imageCenter = new Point();
        double totalAngle = 0;


        protected override void OnMouseLeftButtonDown(System.Windows.Input.MouseButtonEventArgs e)
        {
            base.OnMouseLeftButtonDown(e);

            if (isToMove == false)
            {
                previousPosition = e.GetPosition(this);
                previousMargin = this.Margin;
                isToMove = true;
                imageCenter.X = previousMargin.Left + this.ActualWidth / 2;
                imageCenter.Y = previousMargin.Top + this.ActualHeight / 2;
            }
            else
            {
                previousPosition.X = 0;
                previousPosition.Y = 0;
                isToMove = false;
            }
        }

        protected override void OnMouseLeftButtonUp(MouseButtonEventArgs e)
        {
            base.OnMouseLeftButtonUp(e);

            previousPosition.X = 0;
            previousPosition.Y = 0;
            isToMove = false;
        }

        //移动板块时用
        protected override void OnMouseMove(MouseEventArgs e)
        {
            base.OnMouseMove(e);

            if (isToMove)
            {
                Point currentPosition = e.GetPosition(this);
                if (currentPosition == previousPosition)
                    return;

                if ((currentPosition.X - previousPosition.X > 10 || currentPosition.Y - previousPosition.Y > 10) &&
                    (previousPosition.X != 0 || previousPosition.Y != 0))
                {
                    previousPosition.X = 0;
                    previousPosition.Y = 0;
                    isToMove = false;
                    return;
                }
                double vectorAngle = Vector.AngleBetween(new Vector(previousPosition.X - 200,
                        previousPosition.Y), new Vector(currentPosition.X - 200, currentPosition.Y));
                totalAngle += vectorAngle;

                Thickness currentMargin = new Thickness();
                currentMargin.Left = previousMargin.Left + currentPosition.X - previousPosition.X;
                currentMargin.Top = previousMargin.Top + currentPosition.Y - previousPosition.Y;
                this.Margin = currentMargin;
                previousMargin = currentMargin;

                RotateTransform imageRT = new RotateTransform(totalAngle, 200, 0);
                this.RenderTransform = imageRT;
            }
        }

        protected override void OnLostFocus(RoutedEventArgs e)
        {
            base.OnLostFocus(e);

            isToMove = false;
        }

        protected override void OnGotFocus(RoutedEventArgs e)
        {
            base.OnGotFocus(e);

            isToMove = false;
        }
    }
}
